package cn.engage.ekcc.service.impl;

import cn.engage.ekcc.domain.Product;
import cn.engage.ekcc.domain.ProductBuy;
import cn.engage.ekcc.domain.ProductLog;
import cn.engage.ekcc.exception.GobalException;
import cn.engage.ekcc.fegin.ProductFeginCliect;
import cn.engage.ekcc.fegin.SellFeginCliect;
import cn.engage.ekcc.mapper.ProductBuyMapper;
import cn.engage.ekcc.result.JsonResult;
import cn.engage.ekcc.service.IProductBuyService;
import cn.engage.ekcc.vo.BuyVo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-18
 */
@Service
public class ProductBuyServiceImpl extends ServiceImpl<ProductBuyMapper, ProductBuy> implements IProductBuyService {
    @Autowired
    private ProductBuyMapper productBuyMapper;
    @Autowired
    private SellFeginCliect sellFeginCliect;
    @Autowired
    private ProductFeginCliect productFeginCliect;

    /* 采购商品 */
    @Override
    public void CG(BuyVo buyVo) {
        ProductBuy productBuy = productBuyMapper.selectById(buyVo.getId());
        if (productBuy == null){
            throw new GobalException("该采购清单不存在");
        }
        ProductLog productLog = new ProductLog();
        productLog.setProductId(buyVo.getProductId());
        if (buyVo.getType() == 1){
            // 采购审核成功,记录日志
            productLog.setLog("采购审核成功采购数量为:" + buyVo.getNumber());
            sellFeginCliect.saveOrUpdate(productLog);
            // 进行入库操作
            Product product = new Product();
            product.setId(buyVo.getProductId());
            product.setProductNumber(buyVo.getNumber());
            productFeginCliect.RK(product);
        }else {
            // 采购失败,记录失败审核日志
            productLog.setLog("采购审批失败!");
            sellFeginCliect.saveOrUpdate(productLog);
        }
        // 修改数据库审核状态
        productBuy.setType(buyVo.getType());
        productBuyMapper.updateById(productBuy);
    }
}
